<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('./src/classes/Page.php');
	
	$page = new Page('Index');
	
	$page->print_header();
?>
	<h1 class="pageheading">Forum Index</h1><br /><br />
	
	<div id="forums">
	
	<div class="forum-2">
		<div class="forumname">Name</div>
		<div class="forumtopics"># Topics</div>
		<div class="forumposts"># Posts</div>
		<div class="forumlatestpost">Latest Post</a></div>
	</div>
<?php
		// BEGIN DISPLAY GENERAL FORUMS (without a category)
		$result = $page->connection->query("SELECT * FROM forum WHERE categoryid=-1 AND parentid=-1 ORDER BY displayorder ASC");
		
		if (! $result)
		{
			die($page->connection->get_error());
		}
		
		display_category($page, "General", $result);
		$result->free();
		// END DISPLAY GENERAL FORUMS (without a category)
		
		// BEGIN DISPLAY CATEGORISED FORUMS
		$result = $page->connection->query("SELECT * FROM category ORDER BY displayorder ASC");
		
		if (! $result)
		{
			die($page->connection->get_error());
		}
		
		while ($obj = $result->fetch_object())
		{
			$forums = $page->connection->query("SELECT * FROM forum WHERE categoryid=$obj->id AND parentid=-1 ORDER BY displayorder ASC");
		
			if (! $result)
			{
				die($page->connection->get_error());
			}
			
			display_category($page, $obj->name, $forums);
			
			$forums->free();
		}
		
		$result->free();
		// END DISPLAY CATEGORISED FORUMS
?>
	</div>
<?php
	$page->print_footer();
	
	function display_category($page, $name, $rows)
	{
		$i = 1;
?>
		<h3 class="categoryname"><?php echo $name; ?></h3>
<?php
		while ($obj = $rows->fetch_object())
		{
			$result = $page->connection->get_result("SELECT * FROM post WHERE forumid='$obj->id'");
			$posts = ($result) ? $result->num_rows : 0;
			if ($result)
				$result->free();
			
			$result = $page->connection->get_result("SELECT * FROM topic WHERE forumid='$obj->id'");
			$topics = ($result) ? $result->num_rows : 0;
			if ($result)
				$result->free();
			
			// BEGIN GET LATEST POST
			$latestpost = "";
			$row = $page->connection->get_result_array("SELECT * FROM post WHERE forumid='$obj->id' ORDER BY id DESC LIMIT 0, 1");
			
			if (! empty($row))
			{
				$posterinfo = $page->connection->get_result_array("SELECT username FROM user WHERE id=" . $row['posterid']);
				
				if (! empty($posterinfo))
					$latestpost .= $posterinfo['username'] . ": ";
				
				$topicinfo = $page->connection->get_result_array("SELECT name FROM topic WHERE id=" . $row['topicid']);
				
				if (! empty($topicinfo))
					$latestpost .= $topicinfo['name'];
			}
			
			if ($latestpost == "")
					$latestpost = "-";
			// END GET LATEST POST
?>
			<div class="forum-<?php echo $i; ?>">
				<div class="forumname"><a href="viewforum.php?id=<?php echo $obj->id; ?>&page=1"><?php echo $obj->name; ?></a></div>
				<div class="forumtopics"><?php echo $topics; ?></div>
				<div class="forumposts"><?php echo $posts; ?></div>
				<div class="forumlatestpost"><?php echo $latestpost; ?></a></div>
			</div>
<?php
			if ($i == 1)
			{
				$i = 2;
			}
			else
			{
				$i = 1;
			}
		}
	}
?>